package com.neuedu.mvc.controller;

import com.alibaba.fastjson.JSON;
import com.neuedu.mvc.entity.Department;
import com.neuedu.mvc.service.DepartmentService;
import com.neuedu.mvc.util.JSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet( value = "/axios_dept")
public class AxiosDepartmentController extends HttpServlet {

    DepartmentService departmentService = new DepartmentService();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }



    /**
     *   列表： http://localhost:8080/mvc/axios_dept?_type=list
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String type = request.getParameter("_type");
             if("list".equals(type)){
                this.list(request,response);
            }else if("save".equals(type)){
                this.save(request,response);
            }else if("toEdit".equals(type)){
                 this.toEdit(request,response);
             }
             else if("delete".equals(type)){
                this.deleteById(request,response);
            }
        } catch (Exception throwables) {  //
            throwables.printStackTrace();
        }
    }


    //列表的方法
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException {

        //根据条件查询
        List<Department> departmentList = departmentService.list(request);

        //输出到 客户端
        JSONUtils.write(response,departmentList);


    }


    /**
     * 保存
     * http://localhost:8080/mvc/axios_dept?_type=save&name=xxx&leader=xxx&address=xxx
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, SQLException, ClassNotFoundException {

        String id = request.getParameter("id");

        boolean success = false;
        if(id!= null && !"".equals(id) && !"null".equals(id)){
            success = departmentService.update(request);

        }else{
            success = departmentService.save(request);
        }


        //输出到 客户端
        JSONUtils.write(response,success);

    }

    /**
     * 修改前的 查看信息
     * http://localhost:8080/mvc/axios_dept?_type=toEdit&id=xxx
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws ServletException
     */
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ClassNotFoundException, ServletException {

        Department department = departmentService.queryById(request);
        JSONUtils.write(response,department);
    }

    /**
     * 删除一条数据  http://127.0.0.1:8080/mvc/axios_dept?_type=delete&id=?????
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ClassNotFoundException {

        boolean success = departmentService.removeById(request);

        JSONUtils.write(response,success);
    }




}
